package com.meishij;

import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.Manifest;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.app.AppCompatDelegate;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.jash.myutils.utils.MyOpenHelper;
import com.meishij.customerview.MainQuitDialog;
import com.meishij.entry.RecommendEntry;
import com.meishij.fragment.FindFragment;
import com.meishij.fragment.MallFragment;
import com.meishij.fragment.MineFragment;
import com.meishij.fragment.RecommendFragment;
import com.meishij.fragment.ShiHFragment;
import com.meishij.fragment.ShiHuaFragment;

import junit.framework.Test;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {

    public static final String ACTION_NAME = "changeMode";

    private RadioGroup radioGroup;
    private FrameLayout main_layout;
    private RecommendFragment recommendFragment;
    private FindFragment findFragment;
    private MallFragment mallFragment;
    private ShiHuaFragment shiHuaFragment;
    private MineFragment mineFragment;
    private ShiHFragment shiHFragment;

    private Fragment currentFrag;
    private String backStackName;
    private Intent intent;
    private MyOpenHelper myOpenHelper;
    private SQLiteDatabase database;


    private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (action.equals(ACTION_NAME)) {
                int currentNightMode = getResources().getConfiguration().uiMode
                        & Configuration.UI_MODE_NIGHT_MASK;

                switch (currentNightMode) {
                    case Configuration.UI_MODE_NIGHT_NO:
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                        break;
                    case Configuration.UI_MODE_NIGHT_YES:
                        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                        break;
                }
                recreate();
            }
        }

    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mBroadcastReceiver);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        checkOrGivePermisson();
    }

    private void init() {
        setContentView(R.layout.activity_main);
        myOpenHelper = new MyOpenHelper(this, "recommenddata");
        database = myOpenHelper.getWritableDatabase();
        intent = getIntent();
        radioGroup = ((RadioGroup) findViewById(R.id.radiogroup));
        main_layout = ((FrameLayout) findViewById(R.id.main_container));
        radioGroup.setOnCheckedChangeListener(this);
        ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
        registerBoradcastReceiver();
    }

    private void checkOrGivePermisson() {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if (requestCode == 100 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
        } else {
            Toast.makeText(MainActivity.this, "未获得权限", Toast.LENGTH_SHORT).show();
        }
    }

    public void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTION_NAME);
        registerReceiver(mBroadcastReceiver, myIntentFilter);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {

        switch (checkedId) {
            case R.id.rb_frag_recommend:
                if (recommendFragment == null) {
                    recommendFragment = new RecommendFragment();
                    Bundle bundle = new Bundle();
                    RecommendEntry responseData = (RecommendEntry)intent.getSerializableExtra("responseData");
                    bundle.putSerializable("responseData", responseData);
                    recommendFragment.setArguments(bundle);
                }
                currentFrag = recommendFragment;
                backStackName = "推荐";
                ((RadioButton) radioGroup.getChildAt(0)).setChecked(true);
                break;
            case R.id.rb_frag_find:
                if (findFragment == null) {
                    findFragment = new FindFragment();
                }
                currentFrag = findFragment;
                backStackName = "发现";
                ((RadioButton) radioGroup.getChildAt(1)).setChecked(true);
                break;
            case R.id.rb_frag_mall:
                if (mallFragment == null) {
                    mallFragment = new MallFragment();
                }
                currentFrag = mallFragment;
                backStackName = "商城";
                ((RadioButton) radioGroup.getChildAt(2)).setChecked(true);
                break;
            case R.id.rb_frag_shihua:
                if (shiHFragment == null) {
                    shiHFragment = new ShiHFragment();
                }
                currentFrag = shiHFragment;
                backStackName = "食话";
                ((RadioButton) radioGroup.getChildAt(3)).setChecked(true);
                break;
            case R.id.rb_frag_mine:
                if (mineFragment == null) {
                    mineFragment = new MineFragment();
                }
                currentFrag = mineFragment;
                backStackName = "我的";
                ((RadioButton) radioGroup.getChildAt(4)).setChecked(true);
                break;
        }
        getSupportFragmentManager().beginTransaction().replace(R.id.main_container, currentFrag)
                .addToBackStack(backStackName).commit();

    }
}
